#include <jni.h>
#include <string>
#include <android/log.h>
#include<fcntl.h>
#include<pthread.h>
#include<stdio.h>      /*标准输入输出定义*/
#include<stdlib.h>     /*标准函数库定义*/
#include<unistd.h>     /*Unix 标准函数定义*/
#include<sys/types.h>
#include<sys/stat.h>
#include<termios.h>    /*PPSIX 终端控制定义*/
#include<errno.h>      /*错误号定义*/
#define  LOG_TAG    "native-lib"
#define  LOGI(...)  __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define  LOGE(...)  __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)


extern "C"
JNIEXPORT jstring JNICALL
Java_com_waidi_clothespad_ClothesMainActivity_stringFromJNI(
        JNIEnv *env,
        jobject /* this */) {
    std::string hello = "Hello from C++ ";
    return env->NewStringUTF(hello.c_str());
}

extern "C"
JNIEXPORT jint JNICALL
Java_com_waidi_clothespad_ClothesMainActivity_initSerialPort(JNIEnv *env,
                                                             jobject /* this */)
{


}


extern "C"
JNIEXPORT jint JNICALL
Java_com_waidi_clothespad_ClothesMainActivity_setTime(JNIEnv *env,jint ms,
                                                             jobject /* this */)
{


}


extern "C"
JNIEXPORT jint JNICALL
Java_com_waidi_clothespad_websocket_MyWebsocketClient_setTime(JNIEnv *env,jlong time,
                                                      jobject /* this */)
{

    jlong timesec = time/1000;

   // printf("sss %ld\n",timesec);
    LOGI("%l ", timesec);





   // time_t t;
   // t = timesec;

   // t = 200000;
   // struct tm *p =NULL;
    //p=gmtime(&t);
    //char s[100]={0};

    //strftime(s, sizeof(s), "%Y-%m-%d %H:%M:%S", p);
   // printf("date: %d: %s\n", (int)t, s);
   // sprintf(s,"date -s \"%d-%d-%d %d:%d:%d\"",p->tm_year+1900,p->tm_mon+1,
   // p->tm_mday,p->tm_hour+8,p->tm_min,p->tm_sec);

    //LOGI("date:%s\n",s);
   // system(s);


    LOGI("timesec:%l\n",timesec);

    struct timeval *tv;


    struct timezone *tz;

    gettimeofday (tv, tz);


    tv->tv_sec = timesec;
    tv->tv_usec = 0;

   // tz->tz_dsttime = 0;
   // tz->tz_minuteswest = -480; //不使用
    int r = settimeofday(tv,tz);


    LOGI("tv:%d  tz:%d r:%d\n",tz->tz_dsttime,tz->tz_minuteswest,r);

    if(errno == EPERM)
    {
        LOGI("EPERM\n");
    } else if (errno == EINVAL)
    {
        LOGI("EINVAL\n");
    }
    else if( errno == EPERM)
    {
        LOGI("EPERM\n");
    }

    return 0;

}
